package com.xiaoxian.system.controller;

import com.xiaoxian.basic.util.AjaxResult;
import com.xiaoxian.basic.util.PageList;
import com.xiaoxian.org.domain.Department;
import com.xiaoxian.org.query.DepartmentQuery;
import com.xiaoxian.org.service.IDepartmentService;
import com.xiaoxian.system.domain.DictionaryDetail;
import com.xiaoxian.system.query.DictionaryDetailQuery;
import com.xiaoxian.system.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dict")
@Api(value = "数字字典得api",description="数字字典相关的CRUD功能")//打在Controller对Controller做描述
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * @GetMapping("/{id}") 查询一条  一般使用于查询
     * @PathVariable("获取指定那个参数")   从url中获取id  参数
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )//打在方法上，对方法做描述
    @GetMapping("/{id}")
    public DictionaryDetail queryOne(@PathVariable("id") Long id){
        return dictionaryDetailService.queryOne(id);
    }

    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return dictionaryDetailService.queryAll();
    }
    /**
     * 增删和修改
     * @PutMapping 一般用于增删和修改
     * @param dictionaryDetail
     */
    @PutMapping
    public AjaxResult save(@RequestBody @Valid DictionaryDetail dictionaryDetail){
        System.out.println(dictionaryDetail);
        if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.add(dictionaryDetail);
                System.out.println(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.my();

    }

    /**
     * 通过id删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){

        dictionaryDetailService.delete(id);
            return AjaxResult.my();

    }

    /**
     * 分页查询
     * @param dictionaryDetailQuery
     * @return
     */
    @PostMapping
    public PageList<DictionaryDetail> queryList(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        System.out.println(dictionaryDetailQuery);
        return dictionaryDetailService.queryPage(dictionaryDetailQuery);

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){

        dictionaryDetailService.patchDelete(ids);
            return AjaxResult.my();

    }



}
